package com.scalar.chiptrack.operations.tapereel.actioncommands;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.process.dao.NewVendorLocationDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.release.ContactPerson;
import com.scalar.chiptrack.operations.release.ReleaseHelper;
import com.scalar.chiptrack.operations.release.dao.RelaseDAO;
import com.scalar.chiptrack.operations.release.forms.ReleaseInfoSetupForm;
import com.scalar.chiptrack.operations.tapereel.TapeReelRequest;
import com.scalar.chiptrack.operations.tapereel.dao.TapeReelRequestDAO;
import com.scalar.chiptrack.operations.tapereel.forms.TapeReelRequestForm;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Apr 16, 2007
 * Time: 6:24:38 PM
 */
public class TapeReelRequestCreateCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/tapeReelRequest.do";

    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        TapeReelRequestForm tapeReelRequestForm     = ( TapeReelRequestForm ) form;
        ReleaseInfoSetupForm releaseInfoSetupForm   = new ReleaseInfoSetupForm();
        TapeReelRequest tapeReelRequest             = tapeReelRequestForm.getTapeReelRequest();
        ActionForward actionForward                 = mapping.findForward( "createRequest" );
        HttpSession mSession                        = request.getSession();
        User user                                   = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );
        HashMap<String,String> errors                              = new HashMap<String,String>();
        HashMap<String,String> info                                = new HashMap<String,String>();
        ArrayList lotsList                          = null;
        ArrayList<ContactPerson> vendorContactPersonsList          = null;
        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( ERROR_MESSAGES, errors );

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }
        else
        {
            if ( user.isActionAllowed ( Functionality.TAPE_REEL_REQUEST , User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute ( "showMessage", "FALSE" );
            }
            else
            {
                request.setAttribute ( "showMessage", "TRUE" );
            }
        }

        tapeReelRequest.setReqStatus( TapeReelRequest.STATUS_CREATE );
        //retreiving new work order number
        tapeReelRequest.setWorkOrderNo( TapeReelRequestDAO.genarateAutomatedTapeReelWORequestNumber( 1 ) );

        //retreiving the req date as current date
        tapeReelRequest.setReqDate( StringUtils.formatDateToString( new Date(), "MM/dd/yyyy" ) );
        tapeReelRequest.setTrPartNumber( tapeReelRequestForm.getTrPartNumber() );
        tapeReelRequest.setVendor( TapeReelRequestDAO.getVendorName( tapeReelRequest.getTrlocationId() ) );
        tapeReelRequest.setVendorId( NewVendorLocationDAO.getVendorId( null, tapeReelRequest.getTrlocationId() ) );

        //setting the part number
        tapeReelRequest.setTrPartNumber( ProductDao.getProductNameByProcessStep_Id( tapeReelRequest.getTrProcessStepId() ) );
        tapeReelRequest.setPinCount( TapeReelRequestDAO.getProcessParameter( tapeReelRequest.getTrProcessStepId(), "PIN" ) );
        tapeReelRequest.setPackageType( TapeReelRequestDAO.getProcessParameter( tapeReelRequest.getTrProcessStepId(), "PACKAGE ID" ) );
        tapeReelRequest.setReelQty( TapeReelRequestDAO.getReelQty( tapeReelRequest.getTrProcessStepId() ) );
        //vendorContactPersonsList = TapeReelRequestDAO.loadTapeReelAlertMails( null, tapeReelRequest );

        //vendorContactPersonsList = NewVendorLocationDAO.loadVendorContactPersonsList( null,  tapeReelRequest.getVendorId(), LOCATION_TYPE_FINISHED_GOODS , -1 );
        releaseInfoSetupForm = null;
        releaseInfoSetupForm = new ReleaseInfoSetupForm();
        releaseInfoSetupForm.setVendorId(tapeReelRequest.getVendorId());
        releaseInfoSetupForm.setCategory("PROD");
        releaseInfoSetupForm = RelaseDAO.loadReleaseInfoSetup("Class Store", releaseInfoSetupForm);

        vendorContactPersonsList = RelaseDAO.loadVendorContactPersonsList(null, releaseInfoSetupForm.getReleaseInfoSetupId());
        tapeReelRequest.setTrRequestAlerts( ReleaseHelper.getContactPersonsAsString( "email", vendorContactPersonsList ) );
        ArrayList<ContactPerson> contactPersonsList = RelaseDAO.loadContactPersonsList( releaseInfoSetupForm.getReleaseInfoSetupId() );
        tapeReelRequest.setInternalContactPersonEmail(ReleaseHelper.getContactPersonsAsString( "email", contactPersonsList ) );
        lotsList = TapeReelRequestDAO.getFGLotDetails( tapeReelRequest );

        if ( ( lotsList != null ) && ( lotsList.size() > 0 ) )
        {
            tapeReelRequest.setVendorContactPersonsList( vendorContactPersonsList );
            tapeReelRequest.setContactPersonsList(contactPersonsList);
            tapeReelRequest.setLotList(lotsList);
            tapeReelRequestForm.setTapeReelRequest(tapeReelRequest);
        }
        else
        {
            info.put("No Lots Available for the given search criteria", null);

            request.setAttribute( "partNumbersList", TapeReelRequestDAO.loadTapeAndReelFgProducts() );
            request.setAttribute( "vendorsList", TapeReelRequestDAO.getTrLocations());
            request.setAttribute( "statusList", new ArrayList() );

            actionForward = mapping.findForward( "newRequest" );
        }
        Logger.debug("Fowarding the request to the create page");

        saveToken(request);

        return actionForward;
    }

}
